/**
 * 暂不使用路由复用
 */
import { Injectable } from '@angular/core'
import { Router, RouterStateSnapshot, ActivatedRouteSnapshot, CanDeactivate, CanActivate } from '@angular/router'
import { ReuseTabService } from '@delon/abc'
import { Observable, of } from 'rxjs'

@Injectable({
  providedIn: 'root',
})
export class ListReuseGuardClass implements CanActivate {

  constructor(
    private router: Router,
    private reuseTabService: ReuseTabService,
  ) {}

  canActivate(
    currentRoute: ActivatedRouteSnapshot,
    currentState: RouterStateSnapshot,
  ): Observable<boolean> {
    /**
     * 清除所有不是本 url 的缓存页面
     */
    const currentUrl = currentState.url
    this.reuseTabService.items.forEach(item => {
      if (item.url === currentUrl) {
        return
      }
      this.reuseTabService.close(item.url, true)
    })
    return of(true)
  }
}
